import {
    GetAvatar,
    GetTokenInfo,
    HasNewMessage,
    GetMidNum,
    GetScoreLog,
    GetVipInfo,
    GetNoticeTipsStatus,
} from "~/api";

let messageTimer = null;

export const GET_AVATAR = async () => {
    const userStore = useUserStore();
    if (userStore.isLogin) {
        const res = await GetAvatar();
        userStore.avatar = res.data;
    }
};

export const GET_USERINFO = async () => {
    const userStore = useUserStore();
    if (userStore.isLogin) {
        const res = await GetTokenInfo();
        userStore.userInfo = res.data;
    }
};

export const HAS_NEW_MESSAGE = () => {
    const userStore = useUserStore();
    if (messageTimer) clearTimeout(messageTimer);
    messageTimer = setTimeout(async () => {
        if (userStore.isLogin) {
            const res = await HasNewMessage();
            if (res.code == 200) {
                userStore.messageLog = res.data;
            }
            HAS_NEW_MESSAGE();
        }
    }, 5000);
};

export const GET_MIDNUM = async () => {
    const userStore = useUserStore();
    if (userStore.isLogin) {
        const res = await GetMidNum();
        userStore.midNum = res.data;
    }
};

export const GET_SCORE_LOG = async () => {
    const userStore = useUserStore();
    if (userStore.isLogin) {
        const res = await GetScoreLog();
        userStore.scoreLog = res.data;
    }
};

export const GET_VIPINFO = async () => {
    const userStore = useUserStore();
    if (userStore.isLogin) {
        const res = await GetVipInfo();
        userStore.vipInfo = res.data;
    }
};

export const NoticeTips = async () => {
    const dialogStore = useDialogStore();
    const res = await GetNoticeTipsStatus();

    if (res.code == 200 && !res.data) {
        dialogStore.isNoticeTipsDialogVisible = true;
    }
};

export const GoToPageHome = (uid) => {
    navigateTo(`/homepage/${uid}.html`, {
        open: true
    })
}

export const GetTemplateLink = (url, type) => {
    let newUrl = {
        1: url.replace("medit.vrnewg.com", "medit.ykjhr.com"),
        2: url.replace("medit.vrnewg.com", "kj-medit.ykjhr.com"),
        4: url.replace("medit.vrnewg.com", "xf-medit.ykjhr.com"),
        5: url.replace("medit.vrnewg.com", "zj-medit.ykjhr.com"),
        6: url.replace("medit.vrnewg.com", "kj-medit.ykjhr.com"),
        7: url.replace("medit.vrnewg.com", "yj-medit.ykjhr.com"),
        8: url.replace("medit.vrnewg.com", "medit.ykjhr.com"),
        9: url.replace("medit.vrnewg.com", "yl-medit.ykjhr.com"),
        10: url.replace("medit.vrnewg.com", "kj-medit.ykjhr.com"),
        11: url.replace("medit.vrnewg.com", "medit.ykjhr.com"),
    }[type];

    return newUrl;
}

export const GetTemplateLink2 = (url, type) => {
    let newUrl = {
        template: url.replace("medit.vrnewg.com", "medit.ykjhr.com"),
        courseware: url.replace("medit.vrnewg.com", "kj-medit.ykjhr.com"),
        firefighting: url.replace(
            "medit.vrnewg.com",
            "xf-medit.ykjhr.com"
        ),
        zhanjiang: url.replace("medit.vrnewg.com", "zj-medit.ykjhr.com"),
        xiamen: url.replace("medit.vrnewg.com", "kj-medit.ykjhr.com"),
        yejin: url.replace("medit.vrnewg.com", "yj-medit.ykjhr.com"),
        yiliao: url.replace("medit.vrnewg.com", "yl-medit.ykjhr.com"),
        shuju: url.replace("medit.vrnewg.com", "medit.ykjhr.com"),
        shuiwu: url.replace("medit.vrnewg.com", "kj-medit.ykjhr.com"),
        zonghe: url.replace("medit.vrnewg.com", "medit.ykjhr.com"),
    }[type];

    return newUrl
}

export const GetTemplateLink3 = (type) => {
    let newUrl = {
        1: "medit.ykjhr.com",
        2: "kj-medit.ykjhr.com",
        4: "xf-medit.ykjhr.com",
        5: "zj-medit.ykjhr.com",
        6: "kj-medit.ykjhr.com",
        7: "yj-medit.ykjhr.com",
        8: "medit.ykjhr.com",
        9: "yl-medit.ykjhr.com",
        10: "kj-medit.ykjhr.com",
        11: "medit.ykjhr.com",
    }[type]

    return newUrl
}

export const GetWorkLink = (url, type) => {
    let newUrl = {
        1: url.replace("mshare.vrnewg.com", "mshare.ykjhr.com"),
        2: url.replace("mshare.vrnewg.com", "kj-mshare.ykjhr.com"),
        4: url.replace("mshare.vrnewg.com", "xf-mshare.ykjhr.com"),
        5: url.replace("mshare.vrnewg.com", "zj-mshare.ykjhr.com"),
        6: url.replace("mshare.vrnewg.com", "kj-mshare.ykjhr.com"),
        7: url.replace("mshare.vrnewg.com", "yj-mshare.ykjhr.com"),
        8: url.replace("mshare.vrnewg.com", "mshare.ykjhr.com"),
        9: url.replace("mshare.vrnewg.com", "yl-mshare.ykjhr.com"),
        10: url.replace("mshare.vrnewg.com", "kj-mshare.ykjhr.com"),
        11: url.replace("mshare.vrnewg.com", "mshare.ykjhr.com"),
    }[type];

    return newUrl
}

export const GetWorkLink2 = (url, type) => {
    let newUrl = {
        template: url.replace(
            "mshare.vrnewg.com",
            "mshare.ykjhr.com"
        ),
        courseware: url.replace(
            "mshare.vrnewg.com",
            "kj-mshare.ykjhr.com"
        ),
        firefighting: url.replace(
            "mshare.vrnewg.com",
            "xf-mshare.ykjhr.com"
        ),
        zhanjiang: url.replace(
            "mshare.vrnewg.com",
            "zj-mshare.ykjhr.com"
        ),
        xiamen: url.replace(
            "mshare.vrnewg.com",
            "kj-mshare.ykjhr.com"
        ),
        yejin: url.replace(
            "mshare.vrnewg.com",
            "yj-mshare.ykjhr.com"
        ),
        yiliao: url.replace(
            "mshare.vrnewg.com",
            "yl-mshare.ykjhr.com"
        ),
        shuju: url.replace(
            "mshare.vrnewg.com",
            "mshare.ykjhr.com"
        ),
        shuiwu: url.replace(
            "mshare.vrnewg.com",
            "kj-mshare.ykjhr.com"
        ),
        zonghe: url.replace(
            "mshare.vrnewg.com",
            "mshare.ykjhr.com"
        ),
    }[type];

    return newUrl;
}

export const GetWorkLink3 = (type) => {
    let newUrl = {
        1: "mshare.ykjhr.com",
        2: "kj-mshare.ykjhr.com",
        4: "xf-mshare.ykjhr.com",
        5: "zj-mshare.ykjhr.com",
        6: "kj-mshare.ykjhr.com",
        7: "yj-mshare.ykjhr.com",
        8: "mshare.ykjhr.com",
        9: "yl-mshare.ykjhr.com",
        10: "kj-medit.ykjhr.com",
        11: "medit.ykjhr.com",
    }[type]

    return newUrl;
}